package Tree;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Tree
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-26  08:24
 * @Description: TODO
 * @Version: 1.0
 */
public class 二叉树路径总和112 {


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        // 到了最后的叶子节点
        if (root.left == null && root.right == null) {
            return targetSum == root.val;
        }
        boolean lef_res = hasPathSum(root.left, targetSum - root.val);
        boolean right_res = hasPathSum(root.right, targetSum - root.val);
        // 递归下去
        return lef_res || right_res;
    }
}
